package com.fangyi.yunmusic.service.notification;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class CommentNotificationService {

    public static final String COMMENT_NOTIFICATION = "comment-notification";

    @Autowired
    private StringRedisTemplate redisTemplate;

    public void add(String userId, String commentId) {
        String userKey = convertToKey(userId);
        redisTemplate.opsForSet().add(userKey, commentId);
    }

    public void remove(String userId, String commentId) {
        redisTemplate.opsForSet().remove(convertToKey(userId), commentId);
    }

    public Set<String> getAll(String userId) {
        return redisTemplate.opsForSet().members(convertToKey(userId));
    }

    public Long getSize(String userId) {
        return redisTemplate.opsForSet().size(convertToKey(userId));
    }

    private String convertToKey(String userId) {
        return COMMENT_NOTIFICATION + ":" + userId;
    }
}
